import React from 'react';
import FieldContext from './context';
import { useForm } from './useForm';

const Form = ({ children, form, onFinish }) => {
  const [formInstance] = useForm(form);

  const onSubmit = (e) => {
    e.preventDefault(); // 关闭 表单自动提交表单的属性
    let value = formInstance.submit();
    onFinish(value);
  };

  return (
    <form onSubmit={onSubmit}>
      <FieldContext.Provider value={formInstance}>
        {children}
      </FieldContext.Provider>
    </form>
  );
};

export default Form;
